package gov.pbc.nn.kjgl.AbroadApproval.view;

import gov.pbc.nn.core.dao.PbcnnCoreHibernateDao;
import gov.pbc.nn.core.dao.PbcnnCoreJdbcDao;
import gov.pbc.nn.core.utils.GetTableUtils;
import gov.pbc.nn.kjgl.AbroadApproval.dao.A2TaskDao;
import gov.pbc.nn.kjgl.AbroadApproval.entity.A2TaskEntity;
import gov.pbc.nn.uflo.entity.Opinion;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.annotation.Expose;
import com.bstek.uflo.client.service.ProcessClient;
import com.bstek.uflo.client.service.TaskClient;
import com.bstek.uflo.model.task.Task;


@Component
public class A2CompletePR {

	@Autowired
	private A2TaskDao a2TaskDao;

	@Resource(name = PbcnnCoreHibernateDao.BEAN_ID)
	private PbcnnCoreHibernateDao coreHibernateDao;

	@Resource(name = PbcnnCoreJdbcDao.BEAN_ID)
	private PbcnnCoreJdbcDao corejdbcDao;
	
	@Resource(name = ProcessClient.BEAN_ID)
	private ProcessClient pc;

	@Resource(name = TaskClient.BEAN_ID)
	private TaskClient tc;

	@Expose
	@Transactional
	public void completeTaskZH(Map<String, Object> params) throws Exception {
		long taskId = Long.parseLong(params.get("taskId").toString());
//		String businessId = (String) params.get("businessId");
//		String opinion = (String) params.get("opinion");
		Object agreeObj = params.get("agree");
		int agree = agreeObj == null ? 1 : ((int) agreeObj);

//		String img = (String) params.get("img");
		
		@SuppressWarnings("unchecked")
		Map<String, Object> variables = (Map<String, Object>) params
				.get("variables");
		if (variables == null) {
			variables = new HashMap<String, Object>();
		}
		variables.put("agree", agree);
		
		if(params.get("HldList") != null){
			variables.put("HldList", params.get("HldList"));	
		}
		if(params.get("BbmList") != null){
			variables.put("BbmList", params.get("BbmList"));	
		}
		
//		Task task = this.tc.getTask(taskId);
//
//		final Opinion o = new Opinion();
//		o.setBusKey(businessId);
//		
//
//		if (StringUtils.isNotEmpty(img)) {
//			o.setOpinionImg(img.getBytes());
//		}
//
//		o.setOpinion(opinion);
//
//		o.setBusTable(GetTableUtils.getTablename(A2TaskEntity.class));
//		o.setProcessInstanceId(task.getProcessInstanceId());
//		o.setProcessName("因私出国审批");		
//		o.setTaskId(taskId);
//		o.setTaskName(task.getTaskName());
//		o.setNodeName(task.getNodeName());
//		o.setUsername(ContextHolder.getLoginUserName());
//		o.setCname(ContextHolder.getLoginUser().getCname());
//		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
//		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
//		this.coreHibernateDao.save(o);

		this.tc.start(taskId);
		this.tc.complete(taskId, variables);
	}
	@Expose
	@Transactional
	public void completeTaskLD(Map<String, Object> params) throws Exception {
		long taskId = Long.parseLong(params.get("taskId").toString());
		String businessId = (String) params.get("businessId");
		String opinion = (String) params.get("opinion");
		Object agreeObj = params.get("agree");
		int agree = agreeObj == null ? 1 : ((int) agreeObj);

		String img = (String) params.get("img");
		
		@SuppressWarnings("unchecked")
		Map<String, Object> variables = (Map<String, Object>) params
				.get("variables");
		if (variables == null) {
			variables = new HashMap<String, Object>();
		}
		variables.put("agree", agree);
		
		if(params.get("HldList") != null){
			variables.put("HldList", params.get("HldList"));	
		}
		if(params.get("BbmList") != null){
			variables.put("BbmList", params.get("BbmList"));	
		}
		
		Task task = this.tc.getTask(taskId);

		final Opinion o = new Opinion();
		o.setBusKey(businessId);
		

		if (StringUtils.isNotEmpty(img)) {
			o.setOpinionImg(img.getBytes());
		}

		o.setOpinion(opinion);

		o.setBusTable(GetTableUtils.getTablename(A2TaskEntity.class));
		o.setProcessInstanceId(task.getProcessInstanceId());
		o.setProcessName("因私出国审批");		
		o.setTaskId(taskId);
		o.setTaskName(task.getTaskName());
		o.setNodeName(task.getNodeName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		this.coreHibernateDao.save(o);

//		this.tc.start(taskId);
		this.tc.withdraw(taskId, variables);
	}
	@Expose
	@Transactional
	public void completeTask(Map<String, Object> params) throws Exception {
		long taskId = Long.parseLong(params.get("taskId").toString());
		String businessId = (String) params.get("businessId");
		String opinion = (String) params.get("opinion");
		Object agreeObj = params.get("agree");
		int agree = agreeObj == null ? 1 : ((int) agreeObj);

		String img = (String) params.get("img");
		
		@SuppressWarnings("unchecked")
		Map<String, Object> variables = (Map<String, Object>) params
				.get("variables");
		if (variables == null) {
			variables = new HashMap<String, Object>();
		}
		variables.put("agree", agree);
		
		if(params.get("HldList") != null){
			variables.put("HldList", params.get("HldList"));	
		}
		if(params.get("BbmList") != null){
			variables.put("BbmList", params.get("BbmList"));	
		}
		
		Task task = this.tc.getTask(taskId);

		final Opinion o = new Opinion();
		o.setBusKey(businessId);
		

		if (StringUtils.isNotEmpty(img)) {
			o.setOpinionImg(img.getBytes());
		}

		o.setOpinion(opinion);

		o.setBusTable(GetTableUtils.getTablename(A2TaskEntity.class));
		o.setProcessInstanceId(task.getProcessInstanceId());
		o.setProcessName("因私出国审批");		
		o.setTaskId(taskId);
		o.setTaskName(task.getTaskName());
		o.setNodeName(task.getNodeName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		this.coreHibernateDao.save(o);

		this.tc.start(taskId);
		this.tc.complete(taskId, variables);
	}
	@Expose
	@Transactional
	public void rejectTask(Map<String, Object> params) throws Exception {
		long taskId = Long.parseLong(params.get("taskId").toString());
		String businessId = (String) params.get("businessId");
		String opinion = (String) params.get("opinion");
		Object agreeObj = params.get("agree");
		int agree = agreeObj == null ? 1 : ((int) agreeObj);

		String img = (String) params.get("img");
		
		@SuppressWarnings("unchecked")
		Map<String, Object> variables = (Map<String, Object>) params
				.get("variables");
		if (variables == null) {
			variables = new HashMap<String, Object>();
		}
		variables.put("agree", agree);
		
		if(params.get("HldList") != null){
			variables.put("HldList", params.get("HldList"));	
		}

		
		Task task = this.tc.getTask(taskId);

		final Opinion o = new Opinion();
		o.setBusKey(businessId);
		

		if (StringUtils.isNotEmpty(img)) {
			o.setOpinionImg(img.getBytes());
		}

		o.setOpinion(opinion);

		o.setBusTable(GetTableUtils.getTablename(A2TaskEntity.class));
		o.setProcessInstanceId(task.getProcessInstanceId());
		o.setProcessName("因私出国审批");		
		o.setTaskId(taskId);
		o.setTaskName(task.getTaskName());
		o.setNodeName(task.getNodeName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		this.coreHibernateDao.save(o);

		this.tc.start(taskId);
		this.tc.forward(taskId, "不同意申请");
	}
	
	@Expose
	@Transactional
	public void endTask(Map<String, Object> params) throws Exception {
		long taskId = Long.parseLong(params.get("taskId").toString());
		String businessId = (String) params.get("businessId");
		String opinion = (String) params.get("opinion");
		Object agreeObj = params.get("agree");
		int agree = agreeObj == null ? 1 : ((int) agreeObj);

		String img = (String) params.get("img");
		
		@SuppressWarnings("unchecked")
		Map<String, Object> variables = (Map<String, Object>) params
				.get("variables");
		if (variables == null) {
			variables = new HashMap<String, Object>();
		}
		variables.put("agree", agree);
		
		if(params.get("HldList") != null){
			variables.put("HldList", params.get("HldList"));	
		}

		
		Task task = this.tc.getTask(taskId);

		final Opinion o = new Opinion();
		o.setBusKey(businessId);
		

		if (StringUtils.isNotEmpty(img)) {
			o.setOpinionImg(img.getBytes());
		}

		o.setOpinion(opinion);

		o.setBusTable(GetTableUtils.getTablename(A2TaskEntity.class));
		o.setProcessInstanceId(task.getProcessInstanceId());
		o.setProcessName("因私出国审批");		
		o.setTaskId(taskId);
		o.setTaskName(task.getTaskName());
		o.setNodeName(task.getNodeName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		this.coreHibernateDao.save(o);

		this.tc.start(taskId);
		this.tc.forward(taskId, "审批结束");
	}
	
	@Expose
	@Transactional
	public void copySign(Map<String, Object> params){
		String bus_key = (String) params.get("id");
		String sql = "INSERT INTO t_uflo_opinion SELECT UUID() as ID_, CREATE_DATE_, CREATE_USER_, INVALID_, UPDATE_DATE_, UPDATE_USER_, BUS_KEY_, BUS_TABLE_, CNAME_, DEPT_ID_, DEPT_NAME_, '分管保卫部门行领导意见' AS NODE_NAME_, OPINION_, OPINION_IMG_, PROCESS_INSTANCE_ID_, PROCESS_NAME_, TASK_ID_, '分管保卫部门行领导意见' AS TASK_NAME_, USERNAME_, CREATE_USER_CNAME_, UPDATE_USER_CNAME_ FROM t_uflo_opinion WHERE BUS_KEY_ = ? AND TASK_NAME_ LIKE '%分管行领导意见%'";
		corejdbcDao.getJdbcTemplate().update(sql, new Object[] {bus_key});
	}

}
